module.exports = (sequelize, DataTypes) => {
    const Device = sequelize.define(
        'Device',
        {
            id: {
                type: DataTypes.BIGINT.UNSIGNED,
                allowNull: false,
                autoIncrement: true,
                primaryKey: true,
            },
            no: {
                type: DataTypes.STRING,
                allowNull: false,
                validate: {
                    notNull: {
                        msg: '编号不能为空',
                    },
                },
                comment: '设备编号',
            },
            serial: {
                type: DataTypes.STRING,
                allowNull: false,
                validate: {
                    notNull: {
                        msg: '序列号不能为空',
                    },
                },
                comment: '设备序列号',
            },
            model: {
                type: DataTypes.STRING,
                comment: '设备型号',
            },
            name: {
                type: DataTypes.STRING,
                allowNull: false,
                validate: {
                    notNull: {
                        msg: '设备名不能为空',
                    },
                },
                comment: '设备名字',
            },
            identify: {
                type: DataTypes.STRING,
            },
            ip: {
                type: DataTypes.STRING(15),
                validate: {
                    isIP: {
                        args: true,
                        msg: 'IP地址格式不正确',
                    },
                },
            },
            mac: {
                type: DataTypes.STRING(17),
                validate: {
                    is: {
                        args: /((([a-f0-9]{2}:){5})|(([a-f0-9]{2}-){5}))[a-f0-9]{2}/gi,
                        msg: 'MAC地址格式不正确',
                    },
                },
            },
            begin: {
                type: DataTypes.DATEONLY,
                validate: {
                    isDate: {
                        args: true,
                        msg: '保修开始必须是一个日期格式',
                    },
                },
            },
            end: {
                type: DataTypes.DATEONLY,
                validate: {
                    isDate: {
                        args: true,
                        msg: '保修截止必须是一个日期格式',
                    },
                },
            },
            remark: {
                type: DataTypes.STRING,
                comment: '备注',
            },
            category_id: {
                type: DataTypes.BIGINT.UNSIGNED,
                allowNull: false,
                references: {
                    model: 'Category',
                    key: 'id',
                },
                validate: {
                    notNull: {
                        msg: '所属分类不能为空',
                    },
                },
                comment: '所属分类ID',
            },
            department_id: {
                type: DataTypes.BIGINT.UNSIGNED,
                allowNull: false,
                references: {
                    model: 'Department',
                    key: 'id',
                },
                validate: {
                    notNull: {
                        msg: '所属部门不能为空',
                    },
                },
                comment: '所属部门ID',
            },
            store_id: {
                type: DataTypes.BIGINT.UNSIGNED,
                allowNull: false,
                references: {
                    model: 'Store',
                    key: 'id',
                },
                validate: {
                    notNull: {
                        msg: '所属分店不能为空',
                    },
                },
                comment: '所属店别ID',
            },
        },
        {
            tableName: 'device',
        }
    )

    Device.associate = function (models) {
        models.Device.belongsTo(models.Category, { foreignKey: 'category_id', targetKey: 'id' })
        models.Device.belongsTo(models.Department, { foreignKey: 'department_id', targetKey: 'id' })
        models.Device.hasMany(models.Counter, { foreignKey: 'device_id', sourceKey: 'id' })
        models.Device.hasMany(models.Repair, { foreignKey: 'device_id', sourceKey: 'id' })
    }

    return Device
}
